<?php
namespace Apps\AppApi\Method\BeautyCanon;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Models\CosBeautyCanon;
use Apps\Common\Models\CosBeautyCanonAssist;
/*
 * 美粒宝典列表
 */
class BcList extends BaseMethod{
	private $beautyCanons = [];
	
	public function run(){
	    //上传用户id
	    $user_id = intval($this->getDataItem('user_id',0));
		$page = intval($this -> getDataItem('page',1));
		$limit = $this -> getConfig() -> limit;
		$offset = ($page-1)*$limit;
		
		$this -> beautyCanons = CosBeautyCanon::query()
			->columns([
					'id',
					'title',
					'img_url',
					'settime',
					'fit',
			        'summary',
					'assist'
			])
			-> where("state = 1")
			->orderBy('settime DESC')
			-> limit($limit,$offset)
			-> execute() -> toArray();
		$this -> beautyCanons = $this -> beautyCanons ? $this -> beautyCanons : [];
		
		for($i = 0; $i < count($this -> beautyCanons); $i++) {
		    if ($user_id>0){
        	    //判断是否已经点过赞
        	    $bc_assist = CosBeautyCanonAssist::count([
        	        "conditions"=>"user_id = :sign: AND beauty_canon_id = :bc_id: ",
        	        "bind" => [
        	            'sign' => $user_id,
        	            'bc_id' => $this -> beautyCanons[$i]['id'],
        	        ],
        	    ]);
		    }
		    else{
		        $bc_assist = 0;
		    }
		    if ($bc_assist>0){
		        $this->beautyCanons[$i]['is_assist'] = true;
		    }
		    else{
		        $this->beautyCanons[$i]['is_assist'] = false;
		    }
		    if($this -> beautyCanons[$i]['summary'] == null){
		        $this -> beautyCanons[$i]['summary'] = '';
		    }
            $this -> beautyCanons[$i]['img_url'] = "http://img.meelier.com/".$this -> beautyCanons[$i]['img_url'];;
            if(empty($this -> beautyCanons[$i]['fit'])){
            	$this -> beautyCanons[$i]['fit'] = 0;
            }
        	if(empty($this -> beautyCanons[$i]['assist'])){
        		$this -> beautyCanons[$i]['assist'] = 0;
        	}
		}
		
        $this->setResult( $this -> beautyCanons);
		
		
	}
}